package org.jeecg.modules.bigdatashow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.bigdatashow.entity.BdAccident;
import org.jeecg.modules.bigdatashow.entity.BdAccidentdegree;
import org.jeecg.modules.bigdatashow.entity.BdAccidenttype;
import org.jeecg.modules.bigdatashow.mapper.BdAccidentMapper;
import org.jeecg.modules.bigdatashow.service.IBdAccidentService;
import org.jeecg.modules.bigdatashow.service.IBdAccidentdegreeService;
import org.jeecg.modules.bigdatashow.service.IBdAccidenttypeService;
import org.jeecg.modules.bigdatashow.utils.CommonResult;
import org.jeecg.modules.bigdatashow.utils.GetTimeUtil;
import org.jeecg.modules.bigdatashow.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: 意外事件表
 * @Author: jeecg-boot
 * @Date:   2022-04-05
 * @Version: V1.0
 */
@Service
public class BdAccidentServiceImpl extends ServiceImpl<BdAccidentMapper, BdAccident> implements IBdAccidentService {

    @Autowired
    private IBdAccidenttypeService iBdAccidenttypeService;
    @Autowired
    private IBdAccidentdegreeService iBdAccidentdegreeService;

    @Override
    public CommonResult countAccidentEachMonth() {
        List<CountAccidentVo> countAccidentVos =new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            LambdaQueryWrapper<BdAccident> queryWrapper=new LambdaQueryWrapper<>();
            if (i!=12){
                queryWrapper.between(BdAccident::getAccidenttime, GetTimeUtil.getYearAndMonth(i),GetTimeUtil.getYearAndMonth(i+1));
            }else {
                queryWrapper.between(BdAccident::getAccidenttime, GetTimeUtil.getYearAndMonth(i),GetTimeUtil.getLastDayInYear());
            }
            int count = count(queryWrapper);
            CountAccidentVo countAccidentVo =new CountAccidentVo();
            countAccidentVo.setId(i);
            countAccidentVo.setCount(count);
            countAccidentVo.setDescribe("2021年"+i+"月的事故数");
            countAccidentVos.add(countAccidentVo);
        }
        if (countAccidentVos !=null){
            return new CommonResult(200,"查询成功", countAccidentVos);
        }
        return new CommonResult(400,"无相关数据");
    }

    @Override
    public CommonResult countAccidentDegree() {
        List<BdAccidenttype> accidenttypeList = iBdAccidenttypeService.list();
        List<BdAccidentdegree> accidentdegreeList = iBdAccidentdegreeService.list();
        List<AccidentTypeAndDegreeVo> accidentTypeAndDegreeVos=new ArrayList<>();
        int num=1;
        for (BdAccidenttype bdAccidenttype : accidenttypeList) {
            for (BdAccidentdegree bdAccidentdegree : accidentdegreeList) {
                LambdaQueryWrapper<BdAccident> queryWrapper=new LambdaQueryWrapper();
                queryWrapper.eq(BdAccident::getAccidenttype,bdAccidenttype.getId())
                        .eq(BdAccident::getAccidentdegree,bdAccidentdegree.getId());
                int count = count(queryWrapper);
                AccidentTypeAndDegreeVo accidentTypeAndDegreeVo=new AccidentTypeAndDegreeVo();
                accidentTypeAndDegreeVo.setId(num++);
                accidentTypeAndDegreeVo.setAccidentTypeName(bdAccidenttype.getAccidenttypename());
                accidentTypeAndDegreeVo.setAccidentDegreeName(bdAccidentdegree.getAccidentdegree());
                accidentTypeAndDegreeVo.setCount(count);
                accidentTypeAndDegreeVos.add(accidentTypeAndDegreeVo);
            }
        }
        if (accidentTypeAndDegreeVos!=null){
            return new CommonResult(200,"查询成功",accidentTypeAndDegreeVos);
        }else {
            return new CommonResult(400,"无相关记录");
        }
    }

    public List<CountAccidentVo> sortAccidentType(){
        List<CountAccidentVo> countAccidentVoList=new ArrayList<>();
        List<BdAccidenttype> accidenttypeList = iBdAccidenttypeService.list();
        int num=0;
        for (BdAccidenttype bdAccidenttype : accidenttypeList) {
            LambdaQueryWrapper<BdAccident> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(BdAccident::getAccidenttype,bdAccidenttype.getId());
            int count = count(queryWrapper);
            CountAccidentVo countAccidentVo=new CountAccidentVo();
            countAccidentVo.setId(++num);
            countAccidentVo.setCount(count);
            countAccidentVo.setDescribe(bdAccidenttype.getAccidenttypename());
            countAccidentVoList.add(countAccidentVo);
        }
        countAccidentVoList.sort((o1, o2) -> {
            if (o1.getCount()>o2.getCount()){
                return 1;
            }else if (o1.getCount()<o2.getCount()){
                return -1;
            }else {
                return 0;
            }
        });
        return countAccidentVoList;
    }

    @Override
    public CommonResult getMostAccidentType() {
        List<CountAccidentVo> countAccidentVoList = sortAccidentType();
        CountAccidentVo countAccidentVo = countAccidentVoList.get(countAccidentVoList.size()-1);
        if (countAccidentVo!=null){
            return new CommonResult(200,"查询成功",countAccidentVo);
        }else {
            return new CommonResult(400,"无相关数据");
        }
    }


    @Override
    public CommonResult getLeastAccidentType() {
        List<CountAccidentVo> countAccidentVoList = sortAccidentType();
        System.out.println(countAccidentVoList);
        CountAccidentVo countAccidentVo = countAccidentVoList.get(0);
        if (countAccidentVo!=null){
            return new CommonResult(200,"查询成功",countAccidentVo);
        }else {
            return new CommonResult(400,"无相关数据");
        }
    }

    @Override
    public CommonResult getAccidentTypeAndState() {
        List<AccidentTypeAndStateVo> accidentTypeAndStateVoList=new ArrayList<>();
        List<BdAccidenttype> accidenttypeList = iBdAccidenttypeService.list();
        int num=0;
        for (BdAccidenttype accidenttype : accidenttypeList) {
            LambdaQueryWrapper<BdAccident> queryWrapper1=new LambdaQueryWrapper<>();
            LambdaQueryWrapper<BdAccident> queryWrapper2=new LambdaQueryWrapper<>();
            //未处理
            queryWrapper1.eq(BdAccident::getAccidenttype,accidenttype.getId())
                    .eq(BdAccident::getAccidentstate,0);
            //已处理
            queryWrapper2.eq(BdAccident::getAccidenttype,accidenttype.getId())
                    .eq(BdAccident::getAccidentstate,1);
            int unFinishCount = count(queryWrapper1);
            int finishCount = count(queryWrapper2);
            AccidentTypeAndStateVo accidentTypeAndStateVo=new AccidentTypeAndStateVo();
            accidentTypeAndStateVo.setId(++num);
            accidentTypeAndStateVo.setTypeName(accidenttype.getAccidenttypename());
            accidentTypeAndStateVo.setStateUnFinishCount(unFinishCount);
            accidentTypeAndStateVo.setStateFinishCount(finishCount);
            accidentTypeAndStateVoList.add(accidentTypeAndStateVo);
        }
        if (accidentTypeAndStateVoList!=null){
            return new CommonResult(200,"查询成功",accidentTypeAndStateVoList);
        }else {
            return new CommonResult(400,"无相关数据");
        }

    }

    @Override
    public CommonResult getAccidentTypeInMonth() {
        List<AccidentTypeAndDayVo> accidentTypeAndDayVoList=new ArrayList<>();
        int num=1;
        List<BdAccidenttype> accidenttypeList = iBdAccidenttypeService.list();
        for (BdAccidenttype accidenttype : accidenttypeList) {
            for (int i = 1; i <= 12; i++) {
                LambdaQueryWrapper<BdAccident> queryWrapper=new LambdaQueryWrapper<>();
                if (i!=12){
                    queryWrapper.between(BdAccident::getAccidenttime, GetTimeUtil.getYearAndMonth(i),GetTimeUtil.getYearAndMonth(i+1))
                            .eq(BdAccident::getAccidenttype,accidenttype.getId());
                }else {
                    queryWrapper.between(BdAccident::getAccidenttime, GetTimeUtil.getYearAndMonth(i),GetTimeUtil.getLastDayInYear())
                            .eq(BdAccident::getAccidenttype,accidenttype.getId());
                }
                int count = count(queryWrapper);
                AccidentTypeAndDayVo typeAndDayVo=new AccidentTypeAndDayVo();
                typeAndDayVo.setId(num++);
                typeAndDayVo.setDay(i+"月");
                typeAndDayVo.setTypeName(accidenttype.getAccidenttypename());
                typeAndDayVo.setCount(count);
                accidentTypeAndDayVoList.add(typeAndDayVo);
            }
        }if (accidentTypeAndDayVoList!=null){
            return new CommonResult(200,"查询成功",accidentTypeAndDayVoList);
        }else {
            return new CommonResult(400,"无相关数据");
        }
    }

    @Override
    public CommonResult getTotalLoss() {
        QueryWrapper<BdAccident> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("sum(accidentloss) as totalLoss");
        BdAccident accident = getOne(queryWrapper);
        TotalLossVo totalLossVo=new TotalLossVo();
        totalLossVo.setId(1);
        totalLossVo.setTotalLoss(accident.getTotalLoss());
        totalLossVo.setDesc("损失金额总数");
        if (accident!=null){
            return new CommonResult(200,"查询成功",totalLossVo);
        }else {
            return new CommonResult(400,"无相关记录");
        }

    }


}
